package com.quick.develop.flowable.utils;

import com.quick.develop.flowable.constant.BpmConstants;
import com.quick.develop.framework.utils.StringUtils;

import java.util.Objects;
import java.util.Optional;

/**
 * @author junyuan.liu
 * @description:
 * @date 2022/9/15 8:42
 */
public class BpmTaskUtil {
    /** 
     * @description 拼接描述 如果原来的描述包含则忽略，不包含则拼接
     * @param oldDesc 原来的描述
     * @param txt 拼接值
     * @return java.lang.String
     * @author junyuan.liu
     * @date 2022/9/15 8:48
     */ 
    public static String concatDescription(String oldDesc, String txt) {
        String newDec = "";
        if (StringUtils.isEmpty(txt)) {
            return oldDesc;
        }
        if (StringUtils.isNotEmpty(oldDesc)) {
            if (!oldDesc.contains(txt)) {
                newDec = oldDesc + BpmConstants.COMMENT_SEPARATOR + txt;
            }else {
                newDec = oldDesc;
            }
        }else {
            newDec = txt;
        }
        return newDec;
    }

    public static String clearDescription(String oldDesc, String txt) {
        if (Objects.equals(oldDesc, txt)) {
            return null;
        }else if (oldDesc.contains(txt + BpmConstants.COMMENT_SEPARATOR)) {
           return oldDesc.replace(txt + BpmConstants.COMMENT_SEPARATOR, "");

        }else if (oldDesc.contains(BpmConstants.COMMENT_SEPARATOR + txt)) {
            return oldDesc.replace(BpmConstants.COMMENT_SEPARATOR + txt, "");
        }
        return oldDesc;
    }

    /**
     * @description 根据特殊符号获取描述里面的内容
     * @param desc desc
     * @param sp
     *    {@link BpmConstants#PROCESS_NAME_SP}
     *    {@link BpmConstants#COMMENT_SP}
     *    {@link BpmConstants#START_USER_SP}
     * @return java.lang.String
     * @author junyuan.liu
     * @date 2023/12/27 14:51
     */
    public static  String getSpDesc(String desc, String sp) {
        String str = null;
        if (StringUtils.isNotEmpty(desc) && desc.contains(sp)) {
            String[] strs = desc.split(BpmConstants.COMMENT_SEPARATOR);
            for (String s : strs) {
                if (s.contains(sp)) {
                    str = s.replace(sp, "");
                    if (StringUtils.isEmpty(str) || "null".equals(str)) {
                        str = null;
                        continue;
                    }
                    break;

                }
            }
        }

        return Optional.ofNullable(str).map(String::trim).orElse(null);
    }
}
